package 算法.leetcode.offer;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author lchenglong
 * @date 2022/4/28
 */
public class Offer48 {

    public static void main(String[] args) {
        Offer48 instance = new Offer48();
        String s = "abba";

        int ans = instance.lengthOfLongestSubstring(s);
        System.out.println(ans);
    }
    public int lengthOfLongestSubstring(String s) {
        Map<Character,Integer> visited = new LinkedHashMap<>();
        int max = 0;
        int start = 0;
        for (int i=0;i<s.length();i++){
            Integer originIdx = visited.get(s.charAt(i));
            if (originIdx!=null){
                start = Math.max(start,originIdx+1);
            }
            visited.put(s.charAt(i),i);
            max = Math.max(max,i-start+1);
        }
        return max;
    }
}
